﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using IPC.Communication.Framework;
using IPC.Communication.Framework.BasicFramework;
using IPC.Communication.Framework.Core;

namespace IPC.Communication.Framework.Core.Net
{
    public class NetworkXBase : NetworkBase
{
	protected OperateResult SendFileStreamToSocket(Socket socket, string filename, long filelength, Action<long, long> report = null)
	{
		try
		{
			OperateResult result = new OperateResult();
			using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
			{
				result = SendStreamToSocket(socket, stream, filelength, report, reportByPercent: true);
			}
			return result;
		}
		catch (Exception ex)
		{
            if (socket != null)
            {
                socket.Close();
            }
            if (base.LogNet != null)
            {
                base.LogNet.WriteException(ToString(), ex);
            }
			return new OperateResult(ex.Message);
		}
	}

	protected OperateResult SendFileAndCheckReceive(Socket socket, string filename, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
	{
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Expected O, but got Unknown
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Expected O, but got Unknown
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Expected O, but got Unknown
		//IL_00b5: Expected O, but got Unknown
		FileInfo fileInfo = new FileInfo(filename);
		if (!File.Exists(filename))
		{
			OperateResult operateResult = SendStringAndCheckReceive(socket, 0, "");
			if (!operateResult.IsSuccess)
			{
				return operateResult;
			}
            if (socket != null)
            {
                socket.Close();
            }
			return new OperateResult(StringResources.Language.FileNotExist);
		}
		JObject val = new JObject();
		val.Add("FileName", (JToken)(object)new JValue(servername));
		val.Add("FileSize", (JToken)(object)new JValue(fileInfo.Length));
		val.Add("FileTag", (JToken)(object)new JValue(filetag));
		val.Add("FileUpload", (JToken)(object)new JValue(fileupload));
		JObject val2 = (JObject)(object)val;
		OperateResult operateResult2 = SendStringAndCheckReceive(socket, 1, ((object)val2).ToString());
		if (!operateResult2.IsSuccess)
		{
			return operateResult2;
		}
		return SendFileStreamToSocket(socket, filename, fileInfo.Length, sendReport);
	}

	protected OperateResult SendFileAndCheckReceive(Socket socket, Stream stream, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		//IL_0056: Expected O, but got Unknown
		JObject val = new JObject();
		val.Add("FileName", (JToken)(object)new JValue(servername));
		val.Add("FileSize", (JToken)(object)new JValue(stream.Length));
		val.Add("FileTag", (JToken)(object)new JValue(filetag));
		val.Add("FileUpload", (JToken)(object)new JValue(fileupload));
		JObject val2 = (JObject)(object)val;
		OperateResult operateResult = SendStringAndCheckReceive(socket, 1, ((object)val2).ToString());
		if (!operateResult.IsSuccess)
		{
			return operateResult;
		}
		return SendStreamToSocket(socket, stream, stream.Length, sendReport, reportByPercent: true);
	}

	protected OperateResult<FileBaseInfo> ReceiveFileHeadFromSocket(Socket socket)
	{
		OperateResult<int, string> operateResult = ReceiveStringContentFromSocket(socket);
		if (!operateResult.IsSuccess)
		{
			return OperateResult.CreateFailedResult<FileBaseInfo>(operateResult);
		}
		if (operateResult.Content1 == 0)
		{
            if (socket != null)
            {
                socket.Close();
            }
            if (base.LogNet != null)
            {
                base.LogNet.WriteWarn(ToString(), StringResources.Language.FileRemoteNotExist);
            }
			return new OperateResult<FileBaseInfo>(StringResources.Language.FileNotExist);
		}
		OperateResult<FileBaseInfo> operateResult2 = new OperateResult<FileBaseInfo>
		{
			Content = new FileBaseInfo()
		};
		try
		{
			JObject json = JObject.Parse(operateResult.Content2);
			operateResult2.Content.Name = SoftBasic.GetValueFromJsonObject(json, "FileName", "");
			operateResult2.Content.Size = SoftBasic.GetValueFromJsonObject(json, "FileSize", 0L);
			operateResult2.Content.Tag = SoftBasic.GetValueFromJsonObject(json, "FileTag", "");
			operateResult2.Content.Upload = SoftBasic.GetValueFromJsonObject(json, "FileUpload", "");
			operateResult2.IsSuccess = true;
		}
		catch (Exception ex)
		{
            if (socket != null)
            {
                socket.Close();
            }
			operateResult2.Message = "Extra File Head Wrong:" + ex.Message;
		}
		return operateResult2;
	}

	protected OperateResult<FileBaseInfo> ReceiveFileFromSocket(Socket socket, string savename, Action<long, long> receiveReport)
	{
		OperateResult<FileBaseInfo> operateResult = ReceiveFileHeadFromSocket(socket);
		if (!operateResult.IsSuccess)
		{
			return operateResult;
		}
		try
		{
			OperateResult operateResult2 = null;
			using (FileStream stream = new FileStream(savename, FileMode.Create, FileAccess.Write))
			{
				operateResult2 = WriteStreamFromSocket(socket, stream, operateResult.Content.Size, receiveReport, reportByPercent: true);
			}
			if (!operateResult2.IsSuccess)
			{
				if (File.Exists(savename))
				{
					File.Delete(savename);
				}
				return OperateResult.CreateFailedResult<FileBaseInfo>(operateResult2);
			}
			return operateResult;
		}
		catch (Exception ex)
		{
            if (base.LogNet != null)
            {
                base.LogNet.WriteException(ToString(), ex);
            }
            if (socket != null)
            {
                socket.Close();
            }
			return new OperateResult<FileBaseInfo>
			{
				Message = ex.Message
			};
		}
	}

	protected OperateResult<FileBaseInfo> ReceiveFileFromSocket(Socket socket, Stream stream, Action<long, long> receiveReport)
	{
		OperateResult<FileBaseInfo> operateResult = ReceiveFileHeadFromSocket(socket);
		if (!operateResult.IsSuccess)
		{
			return operateResult;
		}
		try
		{
			WriteStreamFromSocket(socket, stream, operateResult.Content.Size, receiveReport, reportByPercent: true);
			return operateResult;
		}
		catch (Exception ex)
		{
            if (base.LogNet != null)
            {
                base.LogNet.WriteException(ToString(), ex);
            }
            if (socket != null)
            {
                socket.Close();
            }
			return new OperateResult<FileBaseInfo>
			{
				Message = ex.Message
			};
		}
	}

    //[AsyncStateMachine(typeof(<SendFileStreamToSocketAsync>d__7))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult> SendFileStreamToSocketAsync(Socket socket, string filename, long filelength, Action<long, long> report = null)
    //{
    //    <SendFileStreamToSocketAsync>d__7 stateMachine = new <SendFileStreamToSocketAsync>d__7();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.filename = filename;
    //    stateMachine.filelength = filelength;
    //    stateMachine.report = report;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

    //[AsyncStateMachine(typeof(<SendFileAndCheckReceiveAsync>d__8))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult> SendFileAndCheckReceiveAsync(Socket socket, string filename, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
    //{
    //    <SendFileAndCheckReceiveAsync>d__8 stateMachine = new <SendFileAndCheckReceiveAsync>d__8();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.filename = filename;
    //    stateMachine.servername = servername;
    //    stateMachine.filetag = filetag;
    //    stateMachine.fileupload = fileupload;
    //    stateMachine.sendReport = sendReport;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

    //[AsyncStateMachine(typeof(<SendFileAndCheckReceiveAsync>d__9))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult> SendFileAndCheckReceiveAsync(Socket socket, Stream stream, string servername, string filetag, string fileupload, Action<long, long> sendReport = null)
    //{
    //    <SendFileAndCheckReceiveAsync>d__9 stateMachine = new <SendFileAndCheckReceiveAsync>d__9();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.stream = stream;
    //    stateMachine.servername = servername;
    //    stateMachine.filetag = filetag;
    //    stateMachine.fileupload = fileupload;
    //    stateMachine.sendReport = sendReport;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

    //[AsyncStateMachine(typeof(<ReceiveFileHeadFromSocketAsync>d__10))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult<FileBaseInfo>> ReceiveFileHeadFromSocketAsync(Socket socket)
    //{
    //    <ReceiveFileHeadFromSocketAsync>d__10 stateMachine = new <ReceiveFileHeadFromSocketAsync>d__10();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

    //[AsyncStateMachine(typeof(<ReceiveFileFromSocketAsync>d__11))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult<FileBaseInfo>> ReceiveFileFromSocketAsync(Socket socket, string savename, Action<long, long> receiveReport)
    //{
    //    <ReceiveFileFromSocketAsync>d__11 stateMachine = new <ReceiveFileFromSocketAsync>d__11();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.savename = savename;
    //    stateMachine.receiveReport = receiveReport;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

    //[AsyncStateMachine(typeof(<ReceiveFileFromSocketAsync>d__12))]
    //[DebuggerStepThrough]
    //protected Task<OperateResult<FileBaseInfo>> ReceiveFileFromSocketAsync(Socket socket, Stream stream, Action<long, long> receiveReport)
    //{
    //    <ReceiveFileFromSocketAsync>d__12 stateMachine = new <ReceiveFileFromSocketAsync>d__12();
    //    stateMachine.<>t__builder = AsyncTaskMethodBuilder<OperateResult<FileBaseInfo>>.Create();
    //    stateMachine.<>4__this = this;
    //    stateMachine.socket = socket;
    //    stateMachine.stream = stream;
    //    stateMachine.receiveReport = receiveReport;
    //    stateMachine.<>1__state = -1;
    //    stateMachine.<>t__builder.Start(ref stateMachine);
    //    return stateMachine.<>t__builder.Task;
    //}

	public override string ToString()
	{
		return "NetworkXBase";
	}
}
}
